Date.prototype.format = function(fmt) {
	var o = {
		"M+": this.getMonth() + 1, //月份 
		"d+": this.getDate(), //日 
		"h+": this.getHours(), //小时 
		"m+": this.getMinutes(), //分 
		"s+": this.getSeconds(), //秒 
		"q+": Math.floor((this.getMonth() + 3) / 3), //季度 
		"S": this.getMilliseconds() //毫秒 
	};
	if (/(y+)/.test(fmt)) {
		fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
	}
	for (var k in o) {
		if (new RegExp("(" + k + ")").test(fmt)) {
			fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
		}
	}
	return fmt;
}

var sys = {
	date: {
		now: function() {
			var argLen = arguments.length;
			// 是否引用了其他日期
			var refDateScript = false;
			var nowDate;
			// 获得参数的最后一个参数值，判断值是否为日期
			if (argLen > 0) {
				var lastVal = arguments[argLen - 1];
				// 从静态日期中获得值
				var tmpVal = dateTypeArgs[lastVal];
				if (tmpVal) {
					// 如果存在则带入
					lastVal = tmpVal.showValue;
					// 引用日期的脚本
					refDateScript = true;
				} else {
					// 如果引用的参数是一个脚本则返回 -1
					if (scriptDataTypeArgs[lastVal]) {
						return {
							val: -1,
							refDateScript: refDateScript
						};
					}
				}

				try {
					nowDate = new Date();
					if (lastVal.indexOf('-') > 0) {
//						var lastArr = lastVal.split('-');
//						nowDate.setFullYear(lastArr[0]);
//						nowDate.setMonth(lastArr[1]);
//						nowDate.setDate(lastArr[2]);
//						nowDate.setMonth(nowDate.getMonth() - 1);
						nowDate = new Date(lastVal);
						
						argLen--;
						if (argLen > 0) {
							var tempArg = [];
							for (var i = 0; i < argLen; i++) {
								tempArg[i] = arguments[i];
							}
							arguments = tempArg;
						}
					}
				} catch (e) {
					console.log(e);
				}
			}

			if (!nowDate)
				nowDate = new Date();
			var format = 'yyyy-MM-dd';

			// 判断数据是否为 日期格式类型
			function checkIsFormat(dateOpertionType) {
				switch (dateOpertionType) {
					case 'yyyy':
					case 'MM':
					case 'M':
					case 'M':
					case 'dd':
					case 'd':
					case 'hh':
					case 'h':
						return true;
				}

				var len = dateOpertionType.length;
				var val = parseInt(dateOpertionType);
				if (len > 3 && isNaN(val)) { // 超过三个字符即为日期格式类型
					return true;
				} else if (typeof(val) == 'number') {
					return false;
				} else {
					var startChar = dateOpertionType.charAt(0);
					var sign = true;
					switch (startChar) {
						case 'F':
						case 'L':
						case '+':
						case '-':
							sign = false;
							break;
						default:
							sign = true;
					}
					return sign;
				}
			}

			function opertionDate(nowDate, dateOpertionType) {
				if (checkIsFormat(dateOpertionType)) {
					format = dateOpertionType;
					return;
				}

				var len = dateOpertionType.length;
				if (len <= 1) {
					return;
				}

				var lastChar = dateOpertionType.charAt(len - 1);
				var firstChar = dateOpertionType.charAt(0);
				var methodName;
				var nowVal = 0;
				var dateSpecialType;
				// 判断字符类型
				switch (lastChar) {
					case 'y': // 年类型
						methodName = 'setFullYear';
						nowVal = nowDate.getFullYear();
						break;
					case 'M': // 月类型
						methodName = 'setMonth';
						nowVal = nowDate.getMonth();
						break;
					case 'd': // 日类型
						methodName = 'setDate';
						nowVal = nowDate.getDate();
						break;
					case 'h': // 小时
						methodName = 'setHours';
						nowVal = nowDate.getHours();
						break;
				}
				var val = parseInt(dateOpertionType);
				if (isNaN(val)) {
					if (!(firstChar == 'F' || firstChar == 'L' || firstChar == '+' || firstChar == '-')) {
						return;
					}
				}

				if (!isNaN(parseInt(firstChar)) || firstChar == 'F' || firstChar == 'L') { // 设置值					
					if (lastChar == 'd') {
						if (firstChar == 'F') { // 当月第一天
							val = 1;
						} else if (firstChar == 'L') { // 当月最后一天
							var year = nowDate.getFullYear(),
								month = nowDate.getMonth(),
								day = nowDate.getDate();
							var tempDate = new Date(year, month + 1, 1);
							tempDate = new Date(tempDate.getTime() - 1000 * 60 * 60 * 24);
							val = tempDate.getDate();
						}
					} else if (lastChar == 'M') {
						// val += 1;
					}
					var script = 'nowDate.' + methodName + '(' + val + ')';
					eval(script);
				} else if (firstChar == '+' || firstChar == '-') { // 在当前值上进行加减
					var script = 'nowDate.' + methodName + '(' + (nowVal + val) + ')';
					eval(script);
				}
			}
			var val;
			if (argLen == 0) {
				val = nowDate.format(format);
			} else if (argLen == 1) {
				var arg = arguments[0];
				if (arg == 'T' || arg == 'DT') {
					format = (arg == 'T') ? 'hh:00' : format + ' hh:00';
					val = nowDate.format(format);
				}
			}
			for (var i = 0; i < argLen; i++) {
				var argVal = arguments[i];
				opertionDate(nowDate, argVal);
			}
			val = nowDate.format(format);

			return {
				val: val,
				refDateScript: refDateScript
			}
		}
	}
}

function javaCallFun(script) {
	try {
		return eval('sys.date.' + script);
	} catch (err) {
		return err;
	}
}